/**
 * Created by yongwang on 2015/9/6.
 */


angular.module('backofficeApp.roleManager.controllers', [])
    .controller('roleCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {
        $scope.currentPage = 1;
        $scope.pageSize = 10;

        //角色分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/role/roleList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.roleList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                setTimeout(function(){
                    for(var i = 0;i < $(".role-menu-list").length;i++){
                        $scope.roleMenu.paint($(".role-menu-list")[i],"edit"+i);
                    }
                },500);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");

            },
            error: function(err){
                //$scope.roleList = null;
            },
            params: {}
        });


        $scope.search = function() {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {};
            if ($scope.desc) body.roleName = $scope.desc;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/role/roleList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                $scope.roleList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;

            }).error(function () {
                $scope.roleList = null;
            });
        };


        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };
        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/role/roleDelete',
                checkObj: $(".item-check"),//checkbox对象
                key: "roleID",
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                    alert("删除成功！");
                },
                error: function(){}
            };
            $scope.pagination.listDelete(params);
        };
        //新增
        $scope.addData = {};
        $scope.roleMenu = new gObj.roleMenu();
        $scope.roleMenu.paint($("#role_menu_list")[0],"");
        $scope.add = function(addData){
            $scope.addData = {};
            addData.menuID = $scope.getAddMenuList($("#role_menu_list").find("input[data-menu-id]"));
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/role/roleAdd',
                addData: addData,
                success: function(data){
                    $scope.pagination.selectPage(1);//插入成功显示第一页
                    $('#doc-modal-add').modal("close");
                    if(data.result.status == 1){
                        alert("新增成功！");
                        $scope.addData = {};
                    }else{
                        alert("新增失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-add').modal("close");
                    alert("新增失败！");
                }
            };
            $scope.pagination.listAdd(params);
        };
        //编辑
        $scope.update = function(updateData,index){
            var uData = {roleID:"",role:{}};
            if(updateData.roleID)uData.roleID = updateData.roleID;
            if(updateData.roleName != null)uData.role.roleName = updateData.roleName;else {alert("请输入角色名！");return;}
            if(updateData.remark)uData.role.remark = updateData.remark;
            uData.role.menuID = $scope.getAddMenuList($($(".role-menu-list")[index]).find("input[data-menu-id]"));
            //uData = deleteObjNull(uData);
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/role/roleEdit',
                updateData: uData,
                success: function(data){
                    var pageNumber = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    $scope.pagination.selectPage(pageNumber);
                    if(data.result.status == 1){
                        alert("修改成功！");
                    }else{
                        alert("修改失败！");
                    }
                },
                error: function(){
                    var number = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    alert("修改失败！");
                }
            };
            $scope.pagination.listUpdate(params);
        };
        //获取
        $scope.getAddMenuList = function(obj){
            var array = [];
            for(var i=0;i < obj.length;i++){
                if(obj[i].checked == true){
                    array.push(obj[i].dataset.menuId);
                }
            }
            return array;
        };
        $scope.getEditMenu = function(data,temp){
            var obj = temp.target;
            var ob = {},parent = $(obj.parentNode.parentNode),inputArray = parent.find("input[data-menu-id]");
            if(data && data.length)
                data.forEach(function(item){
                    ob[item.sysMenuID] = 1;
                });

            for(var i=0;i<inputArray.length;i++){
                if(ob[inputArray[i].dataset.menuId]){
                    parent.find("input[data-menu-id='"+inputArray[i].dataset.menuId+"']").prop("checked",true);
                }
            }
        };
    }]);